package com.atguigu.web.controller.chat;

import com.atguigu.chat.domain.ChatKnowledge;
import com.atguigu.chat.service.IChatKnowledgeService;
import com.atguigu.common.annotation.Log;
import com.atguigu.common.core.controller.BaseController;
import com.atguigu.common.core.domain.AjaxResult;
import com.atguigu.common.core.page.TableDataInfo;
import com.atguigu.common.enums.BusinessType;
import com.atguigu.common.utils.poi.ExcelUtil;
import com.atguigu.ai.service.AiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 知识库管理Controller
 * 
 * @author lixianfeng
 * @date 2024-06-27
 */
@RestController
@Tag(name = "知识库后台管理")
@RequestMapping("/chat/knowledge")
public class ChatKnowledgeController extends BaseController
{
    @Autowired
    private IChatKnowledgeService chatKnowledgeService;

    @Autowired
    private AiService aiService;

    @Operation(summary = "不分页查询知识库管理列表")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:list')")
    @GetMapping
    public TableDataInfo getByProjectId(ChatKnowledge chatKnowledge){
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        return getDataTable(list);
    }

    @Operation(summary = "分页查询知识库管理列表")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatKnowledge chatKnowledge)
    {
        startPage();
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        return getDataTable(list);
    }

    @Operation(summary = "知识库上传")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:add')")
    @PostMapping("upload")
    public AjaxResult upload(ChatKnowledge chatKnowledge, MultipartFile file){

        chatKnowledge.setUserId(getUserId());
        chatKnowledge.setCreateBy(getUsername());
        return success(aiService.upload(chatKnowledge, file));
    }

    @Operation(summary = "知识库删除")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:remove')")
    @DeleteMapping("remove")
    public AjaxResult removeFile(@RequestParam Long projectId, @RequestParam Long knowledgeId){
        aiService.remove(projectId, knowledgeId);
        return success("删除成功");
    }

    @Operation(summary = "导出知识库管理列表")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:export')")
    @Log(title = "知识库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatKnowledge chatKnowledge)
    {
        List<ChatKnowledge> list = chatKnowledgeService.selectChatKnowledgeList(chatKnowledge);
        ExcelUtil<ChatKnowledge> util = new ExcelUtil<ChatKnowledge>(ChatKnowledge.class);
        util.exportExcel(response, list, "知识库管理数据");
    }

    @Operation(summary = "获取知识库管理详细信息")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:query')")
    @GetMapping(value = "/{knowledgeId}")
    public AjaxResult getInfo(@PathVariable("knowledgeId") Long knowledgeId)
    {
        return success(chatKnowledgeService.selectChatKnowledgeByKnowledgeId(knowledgeId));
    }

    @Operation(summary = "新增知识库管理")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:add')")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatKnowledge chatKnowledge)
    {
        return toAjax(chatKnowledgeService.insertChatKnowledge(chatKnowledge));
    }

    @Operation(summary = "修改知识库管理")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:edit')")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatKnowledge chatKnowledge)
    {
        return toAjax(chatKnowledgeService.updateChatKnowledge(chatKnowledge));
    }

    @Operation(summary = "删除知识库管理")
    @PreAuthorize("@ss.hasPermi('chat:knowledge:remove')")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
	@DeleteMapping
    public AjaxResult remove(@RequestBody ChatKnowledge knowledge){
        return toAjax(this.aiService.remove(knowledge.getProjectId(), knowledge.getKnowledgeId()));
    }
}
